import { http } from '@/utils/request'
import type { Category } from './category'

// 账号信息
export interface Account {
  id: number
  user_id: number
  category_id?: number
  title: string
  username?: string  // 解密后的用户名
  password?: string  // 解密后的密码
  username_encrypted?: string  // 加密的用户名
  password_encrypted?: string  // 加密的密码
  website?: string
  notes?: string  // 解密后的备注
  notes_encrypted?: string  // 加密的备注
  icon?: string
  tags?: string
  tags_array?: string[]
  is_favorite: boolean
  encrypt_version: string
  last_used_at?: string
  expires_at?: string
  created_at: string
  updated_at: string
  category?: Category
  domain?: string
  is_password_expired?: boolean
}

// 创建账号数据
export interface CreateAccountData {
  category_id?: number
  title: string
  username_encrypted?: string
  password_encrypted?: string
  website?: string
  notes_encrypted?: string
  icon?: string
  tags?: string
  is_favorite?: boolean
  encrypt_version?: string
  expires_at?: string
}

// 更新账号数据
export interface UpdateAccountData {
  category_id?: number
  title?: string
  username_encrypted?: string
  password_encrypted?: string
  website?: string
  notes_encrypted?: string
  icon?: string
  tags?: string
  is_favorite?: boolean
  encrypt_version?: string
  expires_at?: string
}

// 账号查询参数
export interface AccountQueryParams {
  category_id?: number
  keyword?: string
  is_favorite?: boolean
  page?: number
  limit?: number
}

// 账号统计
export interface AccountStats {
  total: number
  favorite: number
  expired: number
  expiring: number
}

// 批量操作参数
export interface BatchOperationParams {
  account_ids: number[]
}

// 批量更新分类参数
export interface BatchUpdateCategoryParams {
  account_ids: number[]
  category_id?: number
}

/**
 * 获取账号列表
 */
export const getAccounts = (params?: AccountQueryParams) => {
  return http.get<Account[]>('/accounts', params)
}

/**
 * 获取账号详情
 */
export const getAccountDetail = (id: number) => {
  return http.get<Account>(`/accounts/${id}`)
}

/**
 * 创建账号
 */
export const createAccount = (data: CreateAccountData) => {
  return http.post<Account>('/accounts', data, {
    loading: true,
    loadingText: '创建中...'
  })
}

/**
 * 更新账号
 */
export const updateAccount = (id: number, data: UpdateAccountData) => {
  return http.put<Account>(`/accounts/${id}`, data, {
    loading: true,
    loadingText: '更新中...'
  })
}

/**
 * 删除账号
 */
export const deleteAccount = (id: number) => {
  return http.delete(`/accounts/${id}`, {
    loading: true,
    loadingText: '删除中...'
  })
}

/**
 * 切换收藏状态
 */
export const toggleAccountFavorite = (id: number) => {
  return http.put<Account>(`/accounts/${id}/favorite`)
}

/**
 * 复制密码（记录日志）
 */
export const copyAccountPassword = (id: number) => {
  return http.post(`/accounts/${id}/copy-password`)
}

/**
 * 获取收藏账号
 */
export const getFavoriteAccounts = () => {
  return http.get<Account[]>('/accounts/favorites')
}

/**
 * 搜索账号
 */
export const searchAccounts = (keyword: string) => {
  return http.get<Account[]>('/accounts/search', { keyword })
}

/**
 * 获取即将过期的账号
 */
export const getExpiringAccounts = (days: number = 7) => {
  return http.get<Account[]>('/accounts/expiring', { days })
}

/**
 * 获取已过期的账号
 */
export const getExpiredAccounts = () => {
  return http.get<Account[]>('/accounts/expired')
}

/**
 * 批量删除账号
 */
export const batchDeleteAccounts = (accountIds: number[]) => {
  return http.post<{ count: number }>('/accounts/batch-delete', {
    account_ids: accountIds
  }, {
    loading: true,
    loadingText: '删除中...'
  })
}

/**
 * 批量更新分类
 */
export const batchUpdateAccountCategory = (accountIds: number[], categoryId?: number) => {
  return http.put<{ count: number }>('/accounts/batch-category', {
    account_ids: accountIds,
    category_id: categoryId
  }, {
    loading: true,
    loadingText: '更新中...'
  })
}

/**
 * 获取账号统计
 */
export const getAccountStats = () => {
  return http.get<AccountStats>('/accounts/stats')
}

/**
 * 导出账号数据
 */
export const exportAccounts = (categoryId?: number) => {
  return http.get<Account[]>('/accounts/export',
    categoryId ? { category_id: categoryId } : undefined,
    {
      loading: true,
      loadingText: '导出中...'
    }
  )
}